lib.use(['element', 'table', 'form','laydate','tablePlug'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var tablePlug = layui.tablePlug;
    var laydate=layui.laydate;
    var searchFrm = layui.Form('searchFrm');
    var goodsAccountTable;

    //初始表格基础参数
    table.set(common.layui.table.config(tablePlug));

    searchFrm.selectRender();


    laydate.render({elem: '#accountDate' ,range: '至'});


    // 渲染表格
    goodsAccountTable = table.render({
            elem: '#goodsAccountTable'
            , url: common.util.createUrl('/inv/goodsAccount/list')
            , cols: [[
                {type:'checkbox'}
               ,{type: 'numbers', title: '编号' }
                ,{ title: '结账年份', field: 'accountDate' }
                ,{ title: '结账月份', field: 'accountMonth' }
                ,{ title: '仓库名称', field: 'warehouseName' }
                ,{ title: '单据状态', field: 'accountState' ,templet: function (row) { return common.dist.getValue("inv.GoodsAccount_AccountStateEnum",row.accountState);}}
            ]]
    });

    /**
     * 监听行单击事件，进入编辑事件
     */
    table.on('rowDouble(goodsAccountTableFilter)', function(obj){
        update(obj)
    });
    /**
     * 删除
     */
    $(document).on('click','#del',function(){
        del(table.checkStatus('goodsAccountTable'))
    });

    /**
     * 新增
     */
    $(document).on('click','#add',function(){
        location.href = 'goodsAccountForm.html';
    });
    /**
     * 审核
     */
    $(document).on('click','#check',function(){
        check(table.checkStatus('goodsAccountTable'))
    });
    /**
     * 导出
     */
    $(document).on('click','#export',function(){
        var params=common.from.serializeObject('#searchFrm');
        params['pages']=goodsAccountTable.config.page.pages;
        params['limit']=goodsAccountTable.config.page.limit;
        common.util.exportWindows('/sale/goodsAccount/export',params)
    });

    /**
     * 监听搜索条件提交事件
     */
    form.on('submit(searchFilter)', function(data){
        var param = data.field;
        searchTable(param)
        return false;
    });

    /**
     * 查询表格
     * @param params
     */
    function searchTable(params) {
         params=common.from.serializeObject('#searchFrm');
        goodsAccountTable.reload({where: params})
    }

    /**
     * 修改
     * @param obj
     */
    function update(obj) {
        if(!common.auth.hasPermission("inv:goodsAccount:update")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }
        if(postData.length!=1){common.util.error('请选择一条记录');return;}
        location.href = 'goodsAccountForm.html?id=' + postData[0];
    }
    /**
     * 删除
     * @param obj
     */
    function del(obj) {
        if(!common.auth.hasPermission("inv:goodsAccount:delete")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }

        if(postData.length!=1){common.util.error('请选择一条记录');return;}
        layer.confirm('确认删除!', function(index){
                console.log('Del param is', postData);
                common.ajax.post('/inv/goodsAccount/delete', postData, function (resp) {
                    searchTable();
                    layer.close(index);
                })

         });
    }


    /**
     * 审核
     * @param obj
     */
    function check(obj) {



        layer.open({
            type: 2,
            anim: 3,
            content: 'http://www.baidu.com' //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
        });

return ;


        if(!common.auth.hasPermission("inv:goodsAccount:check")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item;
            });
        }else{
            postData=[obj.data]
        }

        if(postData.length!=1){common.util.error('请选择一条记录进行操作');return;}
        common.util.confirm('确认审核!', function(index){
            console.log('Config Check param is', postData);
            common.ajax.get('/inv/goodsAccount/check/'+postData[0].id, null, function (resp) {
                searchTable();
                layer.close(index);
                common.util.success("审核成功")
            })

        });
    }
});
